package org.tcs.com.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.tcs.com.model.User;

import com.googlecode.genericdao.dao.hibernate.original.GenericDAOImpl;

public class UserDAOImpl extends GenericDAOImpl<User, Long> implements UserDAO {

	@Override
	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		super.setSessionFactory(sessionFactory);
	}

	/**
	 * Get the current Hibernate session
	 */
	@Override
	protected Session getSession() {
		return this.getSessionFactory().openSession();
	}

	@Override
	public User getUser(String name) {
		Session session = getSession();
		Query query= session.createQuery("from users where userName = :name");
		query.setParameter("name",name);
		@SuppressWarnings("unchecked")
		List<User> user = query.list();
		return user.size() == 1 ? user.get(0): null;
	}
}
